<?php  
	namespace Home\Controller;
	use Think\Controller;
	//学员管理
	//@author  程传民
	class StudentsController extends AllowController{
		// 在校学员管理    只有教职员工可以查看
		public function index(){
			$users = D("users");

			//所查找出来的，都是学员  状态为2  正常登陆
			//判断是否使用了按姓名查找
			$arr1 = array();
			$arr1['role_id'] = array("BETWEEN",array("8","11"));		//角色id值要大于7的才是学员(班长，组长，学员)
			$arr2 = array('can_login'=>2);			//正常登陆的用户

			if(!empty($_GET['name'])){
				if(!empty($_GET['class'])){
					$arr3 = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where($arr3)->where("name like '%".$_GET['name']."%'")->count();
				}else{
					$tot = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->count();
				}
			}else{
				if(!empty($_GET['class'])){
					$arr3 = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where($arr3)->count();
				}else{
					$tot = $users->where($arr1)->where($arr2)->count();
				}
			}

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	 		
	    	//分页管理中判断是否使用了按姓名查找   班级查找
	    	$search="";
	 		if(!empty($_GET['name'])){

	 			if(!empty($_GET['class'])){

	 				$arr3 = array("class"=>$_GET['class']);
	 				$list = $users->where($arr1)->where($arr2)->where($arr3)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
	 				$search="/name/{$_GET['name']}/class/{$_GET['class']}";
	 			}else{
	    			$list = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
	    			$search="/name/{$_GET['name']}";
	 			}
	 		}else{
	 			if(!empty($_GET['class'])){
	 				$arr3 = array("class"=>$_GET['class']);
	 				$list = $users->where($arr1)->where($arr2)->where($arr3)->limit($page->firstRow,$page->listRows)->select();
	 				$search="/class/{$_GET['class']}";
		 		}else{
		 			$list = $users->where($arr1)->where($arr2)->limit($page->firstRow,$page->listRows)->select();
		 		}
	 		}

	 		$class = D("class");
	 		$classs = $class->select();
	 		$role = D("role");
	 		$roles = $role->select();
	 		$team = D("team");
	 		$teams = $team->select();
	 		foreach($list as $key=>$value){
	 			//角色
	 			foreach($roles as $k=>$v){
	 				if($list[$key]['role_id']==$v['id']){
	 					$list[$key]['role_id'] = $v['name'];
	 				}
	 			}
	 			//状态
	 			if($list[$key]['can_login']==2){
	 				$list[$key]['can_login']="正常登录";
	 			}
	 			//所在小组
	 			if($list[$key]['team']!=0){
		 			foreach($teams as $k=>$v){
		 				if($list[$key]['class']==$v['class']){
			 				if($list[$key]['team']==$v['id']){
			 					$list[$key]['team'] = $v['name'];
			 				}
		 				}
		 			}
	 				
	 			}else{
	 				$list[$key]['team'] = "暂未分组";
	 			}
	 			//所在班级
	 			foreach($classs as $k=>$v){
		 			if($list[$key]['class'] == $v['id']){
		 				$list[$key]['class'] = $v['name'];
		 			}
	 			}
	 		}
			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("search",$search);
			$this->assign("classs",$classs);
			$this->assign("list",$list);
			$this->display("Students/students");

		}

		//新生学员
		public function newStu(){

			$users = D("users");

			//判断是否使用了按姓名查找
			$arr1 = array("role_id"=>12);
			// $arr1['role_id'] = array("",7);		//角色id值要大于7的才是学员(班长，组长，学员)
			$arr2 = array('can_login'=>1);			//新用户
			$search="";
			if(!empty($_GET['name'])){
				if(!empty($_GET['class'])){
					$arr3 = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where($arr3)->where("name like '%".$_GET['name']."%'")->count();
				}else{
					$tot = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->count();
				}
			}else{
				if(!empty($_GET['class'])){
					$arr3 = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where($arr3)->count();
				}else{
					$tot = $users->where($arr1)->where($arr2)->count();
				}
			}

	    	//分页管理
	    	$nums = 5;
	    	$page = new \Think\Page($tot,$nums);

	    	//分页管理中判断是否使用了按姓名查找   班级查找
	    	$search="";
	 		if(!empty($_GET['name'])){

	 			if(!empty($_GET['class'])){

	 				$arr3 = array("class"=>$_GET['class']);
	 				$list = $users->where($arr1)->where($arr2)->where($arr3)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
	 				$search="/name/{$_GET['name']}/class/{$_GET['class']}";
	 			}else{
	    			$list = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
	 				$search="/name/{$_GET['name']}";
	 			}
	 		}else{
	 			if(!empty($_GET['class'])){
	 				$arr3 = array("class"=>$_GET['class']);
	 				$list = $users->where($arr1)->where($arr2)->where($arr3)->limit($page->firstRow,$page->listRows)->select();
	 				$search="/class/{$_GET['class']}";
		 		}else{
		 			$list = $users->where($arr1)->where($arr2)->limit($page->firstRow,$page->listRows)->select();
		 		}
	 		}


			$class = D("class");
	 		$classs = $class->select();
	 		$role = D("role");
	 		$roles = $role->select();
	 		$team = D("team");
	 		$teams = $team->select();
	 		foreach($list as $key=>$value){
	 			//角色
	 			foreach($roles as $k=>$v){
	 				if($list[$key]['role_id']==$v['id']){
	 					$list[$key]['role_id'] = $v['name'];
	 				}
	 			}
	 			//状态
	 			if($list[$key]['can_login']==1){
	 				$list[$key]['can_login']="新账户";
	 			}
	 			//所在小组
	 			if($list[$key]['team']!=0){
		 			foreach($teams as $k=>$v){
		 				if($list[$key]['class']==$v['class']){
			 				if($list[$key]['team']==$v['id']){
			 					$list[$key]['team'] = $v['name'];
			 				}
		 				}
		 			}
	 				
	 			}else{
	 				$list[$key]['team'] = "暂未分组";
	 			}
	 			//所在班级
	 			foreach($classs as $k=>$v){
		 			if($list[$key]['class'] == $v['id']){
		 				$list[$key]['class'] = $v['name'];
		 			}
	 			}
	 		}
	 		
	 		// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
	 		$this->assign("list",$list);
	 		$this->assign("search",$search);
	 		$this->assign("classs",$classs);
	 		$this->display("Students/newStudent");

		}

		//角色修改
		public function edit(){
			$id = I("get.id");
			$arr = array("id"=>$id);
			$users = D("users");	//users表
			$role = D("role");

			$roles = $role->select();
			$list = $users->where($arr)->find();	//详情信息

			//此学生当前角色值
			$sturole = $list['role_id'];

			$this->assign("list",$list);
			$this->assign("sturole",$sturole);
			$this->assign("roles",$roles);
			$this->display("Students/update");
		}

		//修改学员角色
		public function update(){
			//users表中用户字段id
			$users = D("users");
			$class = D("class");
			$arr = array("id"=>I("post.ids"));
			$data = array("role_id"=>I("post.roleid"));


			//先判断是否是进行修改角色为班长
			if(I("post.roleid")==8){
				//查出这个学员所在班级
				$info1 = $users->where($arr)->find();

				//查出这个班级所有学员
				$info2 = $users->where(array("class"=>$info1['class']))->select();
				
				$list = array();
				foreach($info2 as $key=>$value){
					$list[] = $value['role_id'];
				}
				if(in_array(I("post.roleid"),$list)){
					echo "班级已存在班长";
					return;
				}else{
					$info = $users->where($arr)->save($data);
					if($info){
						echo "修改成功";
					}else{
						echo "修改失败";
					}
				}
				
			}else if(I("post.roleid")==9){
				//查出这个学员所在班级
				$info1 = $users->where($arr)->find();

				//查出这个班级所有学员
				$info2 = $users->where(array("class"=>$info1['class']))->select();
				
				$list = array();
				foreach($info2 as $key=>$value){
					$list[] = $value['role_id'];
				}
				if(in_array(I("post.roleid"),$list)){
					echo "班级已存在事务班长";
					return;
				}else{
					$info = $users->where($arr)->save($data);
					if($info){
						echo "修改成功";
					}else{
						echo "修改失败";
					}
				}
			}else{
				$info = $users->where($arr)->save($data);
				if($info){
					echo "修改成功";
				}else{
					echo "修改失败";
				}
			}


		}

		//密码修改
		public function editpass(){
			$arr = array("id"=>I("get.id"));
			$users = D("users");	//users表
			$list = $users->where($arr)->find();	//详情信息

			$this->assign("list",$list);
			$this->display("Students/updatepass");
		}
		//修改学员密码
		public function updatepass(){
			//users表中用户字段id
			$arr = array("id"=>I("post.ids"));
			$data = array("password"=>md5(I("post.pass")));
			$users = D("users");
			$info = $users->where($arr)->save($data);
			if($info){
				echo "修改成功";
			}else{
				echo "修改失败";
			}

		}

		//把用户启用
		public function start(){
			//users中的id值
			$arr = array("id"=>I("post.id"));
			//把此用户的状态改为2  用户正常登陆
			$a = array("can_login"=>2,"role_id"=>10);
			$users = D("users");
			$info = $users->where($arr)->save($a);
			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

		//把用户禁用掉
		public function stop(){
			//users中的id值
			$arr = array("id"=>I("post.id"));
			//把此用户的状态改为0  用户禁止登陆
			$a = array("can_login"=>0);
			$users = D("users");
			$info = $users->where($arr)->save($a);
			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

		//批量启用
		public function action(){
			$users = D("users");
			$arr = $_GET['ids'];

			// $login["can_login"]=2;
			$arr1 = array("can_login"=>2,"role_id"=>10);

			foreach($arr as $key=>$value){
				$arrs = array("id"=>$value);
				$info = $users->where($arrs)->save($arr1);
			}
			echo "成功";
		}

		//批量禁用
		public function stops(){
			$users = D("users");
			$arr = $_GET['ids'];

			$login["can_login"]=0;
			// $as = "";
			foreach($arr as $key=>$value){
				$arrs = array("id"=>$value);
				$info = $users->where($arrs)->save($login);
				// $as.=$value;
			}
			// echo $as;
			echo "成功";
		}


		//查找个人信息
		public function show(){
			// $id = $_SESSION['userid'];
        	
        	$arr = array("id"=>$_GET['id']);
        	$user = D("users");
        	$list = $user->where($arr)->find();

        	if($list['can_login']!=0){
        		$list['can_login'] = "允许登录";
        	}else{
        		$list['can_login'] = "用户已停用";
        	}

            $class = D("class");
            $a = array("id"=>$list['class']);
            $cls = $class->where($a)->find();
            $list['class'] = $cls['name'];

        	$team = D("team");
            $arrs = array("id"=>$list["team"]);
            $res = $team->where($arrs)->find();
            if($res){
               $list["team"] = $res['name'];
            }else{
                $list['team'] = "暂未分组";
            }

            $class = D("class");
            $classs = $class->field("id,name")->select();

            foreach($classs as $v){
                if($list['class'] == $v['id']){
                    $list['class'] = $v['name'];
                }
            }
            // var_dump($list);
            // exit;
            $this->assign("list",$list);
			$this->display("Students/show");
		}

	}
?>